home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc302emb.zip / LONGCALC.C < prev    next >
C/C++ Source or Header  |  1994-03-18  |  3KB  |  145 lines

  1. /*
  2.  * This program implements a simple "calculator" using LONG numbers.
  3.  * It demonstrates the "long" math functions from the library.
  4.  */
  5.  
  6. #include cflea.h
  7.  
  8. /*
  9.  * Default LONG number size is 4 bytes (32 bits). If you wish to change
  10.  * this, look at the LONGMATH.ASM file in the library. You also have to
  11.  * adjust the sizes of all array declarations which are to contain LONG
  12.  * numbers. For this demo, just adjust the constant defined below.
  13.  */
  14. #define    LSIZE    4        /* 32 bit numbers */
  15.  
  16. /*
  17.  * Long number registers
  18.  */
  19.  char reg[LSIZE], temp1[LSIZE],    temp2[LSIZE];
  20.  
  21. /*
  22.  * Input/Output Digit buffer. Note that we use an approximation to
  23.  * compute the number of digits required based on the size of the
  24.  * LONG numbers.
  25.  */
  26.  char buffer[(LSIZE*25)/10+1];
  27.  
  28. /*
  29.  * This temporary variable used by the LONGMATH functions is useful,
  30.  * because it will contain the remainder after a division.
  31.  */
  32. extern  char Longreg[];
  33.  
  34. /*
  35.  * Convert a LONG number into a printable string
  36.  */
  37. long_to_string(string, n1, base)
  38.      unsigned char *string, *n1, base;
  39. {
  40.     unsigned sp;
  41.     unsigned char c, stack[(LSIZE*25)/10+1];
  42.  
  43.     longcpy(temp2, n1);
  44.     longset(temp1, base);
  45.     
  46.     /* Stack up digits in reverse order */
  47.     sp = 0;
  48.     do {
  49.         longdiv(temp2, temp1);
  50.         stack[sp++] = ((c = *Longreg) > 9) ? c + '7' : c + '0'; }
  51.     while(longtst(temp2));
  52.  
  53.     /* Unstack digits into output buffer */
  54.     do
  55.         *string++ = stack[--sp];
  56.     while(sp);
  57.     *string = 0;
  58. }
  59.  
  60. /*
  61.  * Convert a string into a LONG number
  62.  * Returns character terminating conversion.
  63.  */
  64. string_to_long(string, n1, base)
  65.      unsigned char *string, *n1, base;
  66. {
  67.     unsigned char c;
  68.  
  69.     longset(n1, 0);
  70.  
  71.     while(c = *string++) {
  72.         if(isdigit(c))
  73.             c -= '0';
  74.         else if(c >= 'a')
  75.             c -= ('a' - 10);
  76.         else if(c >= 'A')
  77.             c -= ('A' - 10);
  78.         else
  79.             break;
  80.         if(c >= base)
  81.             break;
  82.         longset(temp1, base);
  83.         longmul(n1, temp1);
  84.         longset(temp1, c);
  85.         longadd(n1, temp1); }
  86.     return c;
  87. }
  88.  
  89. /*
  90.  * Main calculator program
  91.  */
  92. main()
  93. {
  94.     unsigned char c, op;
  95.      char *ptr;
  96.  
  97. clear:            /* Clear the calculator by forcing a copy operation */
  98.     putstr("\nReady\n");
  99.     op = '=';
  100.  
  101. next:            /* Begin processing a number */
  102.     ptr = buffer;
  103. next1:            /* Continue processing a number */
  104.     while(isdigit(c = getch())) {        /* Collect digits into string */
  105.         putch(c);
  106.         *ptr++ = c; }
  107.     *ptr = 0;                            /* Zero terminate */
  108.     string_to_long(buffer, temp2, 10);    /* Get LONG value for operation */
  109.  
  110.     switch(op) {                        /* Perform pending operation */
  111.         case '=' :    longcpy(reg, temp2);    break;
  112.         case '+' :    longadd(reg, temp2);    break;
  113.         case '-' :    longsub(reg, temp2);    break;
  114.         case '*' :    longmul(reg, temp2);    break;
  115.         case '/' :    longdiv(reg, temp2);    break;
  116.         case '%' :    longdiv(reg, temp2); longcpy(reg, Longreg); }
  117.  
  118. operate:                                /* Process next operation */
  119.     putch(c);                            /* Echo next command */
  120.     switch(c) {
  121.         default:
  122.             putstr(" Use: + - * / % = C");
  123.         case 'c' :                        /* Clear */
  124.         case 'C' :
  125.             goto clear;
  126.         case '=' :                        /* Display results */
  127.             long_to_string(buffer, reg, 10);
  128.             putstr(buffer);
  129.             if(isdigit(c = getch())) {
  130.                 op = '=';
  131.                 putch('\n');
  132.                 putch(c);
  133.                 ptr = buffer;
  134.                 *ptr++ = c;
  135.                 goto next1; }
  136.             goto operate;
  137.         case '+' :                        /* Addition */
  138.         case '-' :                        /* Subtraction */
  139.         case '*' :                        /* Multiplication */
  140.         case '/' :                        /* Division */
  141.         case '%' :                        /* Modulus */
  142.             op = c; }
  143.     goto next;
  144. }
  145.